let access_database = require("../database/access_database")
let staffSQLStore = require("../database/SQLSTORE_staff")
let assignSQLStore = require("../database/SQLSTORE_mission_assign")
let message = require("./message")
let code_message = require("./code_message.json")
let staff_headImgSQLStore = require("../database/SQLSTORE_staff_headImg")
let Busboy = require('busboy');

module.exports = {
    "catStaffAccount": async function (req, res) {

        try {
            let results = await access_database(staffSQLStore.get("CatStaff"), [req.session.staffid])
            console.log(results)
            res.json(results)
        } catch (e) {
            console.log(e)
        }

    },

    catTodayMission: async function (req, res) {
        try {

            let results = await access_database(assignSQLStore.get("selectMyTodayMission"), [req.session.staffid])
            if (results.length > 0) {
                res.json(new message(223, results, code_message['223']))
            } else {
                res.json(new message(225, null, code_message['225']))
            }
        } catch (e) {
            console.log(e)
        }
    },
    catTomorrowMission: async function (req, res) {
        try {
            let results = await access_database(assignSQLStore.get("selectMyTomorrowMission"), [req.session.staffid])
            console.log(results)
            if (results.length > 0) {
                res.json(new message(224, results, code_message['224']))
            } else {
                res.json(new message(226, null, code_message['226']))
            }
        } catch (e) {
            console.log(e)
        }
    },
    cancelAMission: async function (req, res) {
        try {
            let results = await access_database(assignSQLStore.get("deleteMissionAssign"), [req.session.staffid,req.body.mission_id])
            console.log(results)
            if (results.affectedRows > 0) {
                res.json(new message(227, null, code_message['227']))
            } else {
                res.json(new message(464, null, code_message['464']))
            }
        } catch (e) {
            console.log(e)
            res.json(new message(464,null, code_message['464']))
        }
    },
    catAllAssignMission: async function (req, res) {
        try {
            let results = await access_database(assignSQLStore.get("selectAssignMissionByStaffId"), [req.session.staffid])
            if (results.length > 0) {
                res.json(new message(234, results, eval(code_message['234'])))
            } else {
                res.json(new message(471, null, eval(code_message['471'])))
            }
        } catch (e) {
            console.log(e)
            res.json(new message(472,null, code_message['472']))
        }
    },
    modifyStaffAccount: async function (req, res) {
        if (!req.body.name) {
            res.json(new message(450, null, code_message['450']))
        }  else {
            try {
                let results = await access_database(staffSQLStore.get("updateStaff"), [req.body.name, req.body.email, req.session.staffid])
                if (results.affectedRows > 0) {
                    res.json(new message(212, null, code_message['212']))
                } else {
                    res.json(new message(451, null, code_message['451']))
                }
            } catch (e) {
                console.log(e)
            }
        }

    },
    finishMission: async function (req, res) {
            try {
                let results = await access_database(assignSQLStore.get("updateAssignMission"), [req.session.staffid,req.body.mission_id])
                if (results.affectedRows > 0) {
                    res.json(new message(236, null, code_message['236']))
                } else {
                    res.json(new message(473, null, code_message['473']))
                }
            } catch (e) {
                console.log(e)
                res.json(new message(473, null, code_message['473']))
            }


    },

}